package net.xxcxcxcx.xc_enhancedmob.packet;

import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.Packet;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.xxcxcxcx.xc_enhancedmob.render.particle.ParticleHelper;
import net.xxcxcxcx.xc_enhancedmob.render.particle.summon.CustomSummonParticle;

import static net.xxcxcxcx.xc_enhancedmob.EnhancedMobMod.CustomParticle;

public class CustomParticlePacket {

    public int ID;
    public CustomSummonParticle Particle;

    public CustomParticlePacket(int id, CustomSummonParticle c) {
        ID = id;
        Particle = c;
    }

    public CustomParticlePacket(PacketByteBuf buf) {
        read(buf);
    }

    public static void Updata(CustomSummonParticle customParticle, BlockPos blockPos, ServerWorld serverWorld) {
        for (ServerPlayerEntity player : PlayerLookup.tracking(serverWorld, blockPos)) {
            ServerPlayNetworking.send(player, CustomParticle, write(customParticle));
        }
    }

    public static PacketByteBuf write(CustomSummonParticle customParticle) {
        PacketByteBuf buf = PacketByteBufs.create();
        buf.writeInt(customParticle.getID());
        customParticle.write(buf);
        return buf;
    }

    public static Packet<?> creat(CustomSummonParticle customParticle) {
        return ServerPlayNetworking.createS2CPacket(CustomParticle, write(customParticle));
    }

    public void read(PacketByteBuf buf) {
        ID = buf.readInt();
        Particle = ParticleHelper.MakeParticleForBuf(ID, buf);
    }
}
